package beans;


import database.DatabaseConnection;

import java.io.PrintWriter;

import java.math.BigDecimal;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.event.ValueChangeEvent;

import obj.StudentObject;


public class InfoStudent {


    private StudentObject student=null;
    private boolean campuriReadOnly = true;
    private int restante;

    public InfoStudent() {
        student();
    }

    public void student() {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        PrintWriter pw = null;
        try {

            con = DatabaseConnection.getConectare();
            System.out.println(con);
            stmt = con.createStatement();
            rs =
 stmt.executeQuery(" select id_student,s.nume, p.nume nume_prof,s.id_prof_supervizor id_prof_supervizor, initiala_tatalui, s.prenume, taxa,credite,s.email,s.poza, s.parola,master " +
                   " from student s,profesori p where s.id_prof_supervizor= p.id_prof and s.id_student=" +
                   getBean("#{formularStudent}").getId_student());         
            if (rs.next()) {
                BigDecimal id_student = rs.getBigDecimal("id_student");
                String nume = rs.getString("nume");
                String initiala_tatalui = rs.getString("initiala_tatalui");
                String prenume = rs.getString("prenume");
                String parola = rs.getString("parola");
                BigDecimal credite = rs.getBigDecimal("credite");
                String email = rs.getString("email");
                String poza = rs.getString("poza");
                String master = rs.getString("master");
                String taxa = rs.getString("taxa");
                BigDecimal id_prof = rs.getBigDecimal("id_prof_supervizor");
                String nume_prof = rs.getString("nume_prof");
                student = new StudentObject();
                student.setId_student(id_student);
                student.setNume(nume);
                student.setInitiala_tatalui(initiala_tatalui);
                student.setPrenume(prenume);
                student.setEmail(email);
                student.setParola(parola);
                student.setPoza(poza);
                student.setMaster(master);
                student.setCredite(credite);
                student.setTaxa(taxa);
                student.setNume_prof(nume_prof);
                student.setId_prof_supervizor(id_prof);
            }

        } catch (ClassNotFoundException ex) {

        } catch (SQLException ex) {
            System.out.println("err sql" + ex);

        }

        finally {
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException ex) {

            }
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {

            }
        }
    }


    public String actionEdit() {
        setCampuriReadOnly(false);
        return null;
    }

    public String salveaza() {
        setCampuriReadOnly(true);
        Connection con = null;
        Statement stmt = null;
        PrintWriter pw = null;
        try {
            con = DatabaseConnection.getConectare();
            stmt = con.createStatement();
            String sql =
                " update student set  nume='" + student.getNume() + "', initiala_tatalui='" + student.getInitiala_tatalui() +
                "', prenume='" + student.getPrenume() + "', taxa='" + student.getTaxa() + "',email='" +
                student.getEmail() + "',poza='" + student.getPoza() + "', parola='" + student.getParola() +
                "',master='" + student.getMaster() + "',id_prof_supervizor=" + student.getId_prof_supervizor() +
                "  where id_student=" + getBean("#{formularStudent}").getId_student();
            int x = stmt.executeUpdate(sql); //de modificat


        } catch (ClassNotFoundException ex) {

        } catch (SQLException ex) {
            System.out.println("eroare sql " + ex);
        }

        finally {

            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException ex) {

            }
        }
        return null;
    }


    public void valueChangeListenerProf(ValueChangeEvent valueChangeEvent) {
        student.setId_prof_supervizor((BigDecimal)valueChangeEvent.getNewValue());
        System.out.println("student" + student.getId_prof_supervizor());
    }

    private StudentFormularBB getBean(String bean) {
        // StudentFormularBB bean = (StudentFormularBB) request.getSession().getAttribute(bean);
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        // May be deprecated
        ValueBinding binding = app.createValueBinding(bean);
        Object value = binding.getValue(context);
        return (StudentFormularBB)value;
    }

    public void setRestante(int restante) {
        this.restante = restante;
    }

    public int getRestante() {
        return restante;
    }

    public void setStudent(StudentObject student) {
        this.student = student;
    }

    public StudentObject getStudent() {
        return student;
    }

    public void setCampuriReadOnly(boolean campuriReadOnly) {
        this.campuriReadOnly = campuriReadOnly;
    }

    public boolean isCampuriReadOnly() {
        return campuriReadOnly;
    }

}
